﻿/**
 * @author sminutoli
 * version 1.1
 * tengo que avisar de alguna manera que estoy cargadito. al extender de controlcabeza le tiro un initChild a mi papá
 */
import com.dinmax.flash.central.bridge.CENTRALBridge;
import com.dinmax.flash.central.bridge.EXTENSIONBridge;
import com.dinmax.flash.draw.cabeza.ControlCabeza;
import com.dinmax.flash.utilities.textfield.TextFieldUtils;

class com.dinmax.flash.central.bridge.LOGINBridge 
extends ControlCabeza {
	
	private var input_pass:TextField;
	private var input_user:TextField;
	private var mcGo:MovieClip;
	private var mcPref:MovieClip;
	private var mcHelp:MovieClip;
	private var bridge:CENTRALBridge;
	
		
	function onLoad():Void {
		super.onLoad();
		this.init();
		return;
	};
	
	public function init():Void {
		//trace("LOGINBridge.as init()");
		input_pass.password = true;
		Key.addListener(this.input_pass);
		this.initControl(this.input_user);
		this.initControl(this.input_pass);
		//this.initControl(this.mcPref);
		//this.initControl(this.mcHelp);
		this.initControl(this.mcGo);
		var root:LOGINBridge = this;
		this.mcGo.onPress = function():Void{
			root.validate();
			return;
		};
		return;
	};
		
	public function initControl(control:Object):Void {
		var root:LOGINBridge = this;
		var myDropFilter:flash.filters.DropShadowFilter = new flash.filters.DropShadowFilter();
		myDropFilter.alpha = .5;
	
		if(control instanceof TextField){
			control.borderColor = 0x888888;
				
			control.onSetFocus = function(){
				var myFilters:Array = this.filters;
				myFilters.push(myDropFilter);
				this.filters = myFilters;
				//this.borderColor = 0x00aa00;
				this.onKeyDown = function() {
					if(Key.getAscii() == Key.ENTER) {
						root.validate();
					}
				}
				Key.addListener(this);
				
			}
			control.onKillFocus = function(){
				this.filters = this.filters.pop();
				//control.borderColor = 0x888888;
				delete this.onKeyDown;
				Key.removeListener(this);
			}
					
		} else if(control instanceof MovieClip) {
			control.onRollOver = function (){
				var myFilters:Array = this.filters;
				myFilters.push(myDropFilter);
				this.filters = myFilters;
			}
			control.onRollOut = function (){
				this.filters = [];
			}
		}// end if
	}
		
	public function validate():Void {
		//trace("LOGINBridge.as validate() bridge= "+bridge);
		try {
			//trace("validando los campos");
			var fields:Array = [input_user, input_pass];
			TextFieldUtils.validateFields(fields);
			bridge.logIn(input_user.text, input_pass.text);
		} catch (e) {
			trace("Error validando los campos: "+e);
		};
		
	};
		
	public function setBridge(bridge:CENTRALBridge):Void {
		//trace("LOGINBridge.as | setBridge() bridge= "+bridge);
		this.bridge = bridge;
	};
	
};